package com.example.photography.adapter.community;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.photography.R;
import com.example.photography.bean.CommunityBean;
import com.example.photography.ui.CircleImageView;
import com.example.photography.util.Util;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Community_QuuAdapter extends RecyclerView.Adapter<Community_QuuAdapter.ViewHolder> {
    private final Context context;
    private List<CommunityBean.DataBean.HotCommentBean> hotCommentBeans;
    private String[] bg_color= new String[]{"comment_blue","comment_orange","comment_gray"};

    public Community_QuuAdapter(Context context, List<CommunityBean.DataBean.HotCommentBean> hotCommentBeans) {
        this.context = context;
        this.hotCommentBeans = hotCommentBeans;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_community, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }


    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        int a = position % 5;
//        TypedArray pic = context.getResources().obtainTypedArray(R.array.pic);
//        int id = pic.getResourceId(a,-1);
//        holder.rlQue.setBackgroundColor(id);
        switch (a){
            case 0:
                holder.rlQue.setBackgroundResource(R.drawable.comment_blue);
                break;
            case 1:
                holder.rlQue.setBackgroundResource(R.drawable.comment_orange);
                break;
            case 2:
                holder.rlQue.setBackgroundResource(R.drawable.comment_gray);
                break;
            case 3:
                holder.rlQue.setBackgroundResource(R.drawable.comment_pink);
                break;
            case 4:
                holder.rlQue.setBackgroundResource(R.drawable.comment_yellow);
                break;
        }
        Glide.with(context)
                .load(hotCommentBeans.get(position).getHead_url())
                .thumbnail(Glide.with(context).load(R.drawable.head_empty))
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                .into(holder.cvHead);
        holder.tvName.setText(hotCommentBeans.get(position).getName());
        holder.tvComment.setText(hotCommentBeans.get(position).getComment());
        holder.tvNums.setText(hotCommentBeans.get(position).getNumbers()+"次讨论");

    }

    @Override
    public int getItemCount() {
        return hotCommentBeans == null?0:hotCommentBeans.size();
    }

    static
    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_comment)
        TextView tvComment;
        @BindView(R.id.cv_head)
        CircleImageView cvHead;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_nums)
        TextView tvNums;
        @BindView(R.id.rl_que)
        RelativeLayout rlQue;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
